'use strict';
const Service = require('egg').Service;
class PageService extends Service {
    async findAllPage(user) {
        const { app } = this;
        try {
            const id = user.role_id;
            const role = await app.mysql.select('hsk_role', { where: { id } });
            if(role.length === 0) return null;
            const pages = role[0].pages.split(',');
            const list = await app.mysql.select('hsk_page');
            const newList = list.filter(item => {
                return pages.includes(item.id + '');
            });
            return { list: newList }
          } catch (error) {
            console.log(error);
            return null;
          }
    }

    async saveEditPage() {
        const { app } = this;
        const { id, father, icon, label, path, redirect, sort } = this.ctx.request.body;
        const result = await app.mysql.update('hsk_page', { father, icon, label, path, redirect, sort }, { where: { id } });
        const updateSuccess = result.affectedRows === 1;
        return updateSuccess;
    }
}
module.exports = PageService;
